<?php
namespace app\ruinuo\controller;
use app\common\service\DataBase as DataBaseService;
use app\ruinuo\service\database\Lists as RuinuoLists;
use app\ruinuo\service\database\Logic as RuinuoLogic;
use think\facade\Db;
use think\facade\Request;
use app\common\middleware\Auth;
use app\common\middleware\Power;
use app\common\middleware\Log;
use think\facade\View;

class Database
{
    protected $middleware = [Auth::class => [], Power::class => [], Log::class => []];

    /*列表模板及数据*/
    public function Index()
    {
        if (Request::isAjax() || Request::isPost()) {
            $RuinuoLists = new RuinuoLists();
            $data['rows'] = $RuinuoLists->Lists(input());//列表数据
            $data['total'] = $RuinuoLists->Total(input());//总数据
            return BackCode($data);
        }
        return ViewTpl('ruinuo/database/lists');
    }

    /*Tab及统计*/
    public function TabSum()
    {
        if (Request::isAjax() || Request::isPost()) {
            $RuinuoLists = new RuinuoLists();
            $data['tab'] = $RuinuoLists->Tab(input());//Tab统计
            $data['sum'] = $RuinuoLists->Sum(input());//Sum统计
            return BackCode($data);
        }
    }

    /*明细*/
    public function Detail()
    {
        if (Request::isAjax() || Request::isPost()) {
            $RuinuoLogic = new RuinuoLogic();
            $data = $RuinuoLogic->Detail(input());
            return BackCode($data['data'], $data['code'], $data['msg']);
        }
        return ViewTpl('ruinuo/database/form');
    }

    /*新增数据*/
    public function Add()
    {
        if (Request::isAjax() || Request::isPost()) {
            $RuinuoLogic = new RuinuoLogic();
            $data = $RuinuoLogic->Add(input());
            return BackCode($data['data'], $data['code'], $data['msg']);
        }
        return ViewTpl('ruinuo/database/form');
    }

    /*修改数据*/
    public function Edit()
    {
        if (Request::isAjax() || Request::isPost()) {
            $RuinuoLogic = new RuinuoLogic();
            $data = $RuinuoLogic->Edit(input());
            return BackCode($data['data'], $data['code'], $data['msg']);
        }
        return ViewTpl('ruinuo/database/form');
    }

    /*删除单条数据*/
    public function Delete()
    {
        if (Request::isAjax() || Request::isPost()) {
            $RuinuoLogic = new RuinuoLogic();
            $data = $RuinuoLogic->Delete(input('id'), 1);
            return BackCode($data['data'], $data['code'], $data['msg']);
        }
    }

    /*批量删除数据*/
    public function batch_delete()
    {
        if (Request::isAjax() || Request::isPost()) {
            $RuinuoLogic = new RuinuoLogic();
            $data = $RuinuoLogic->Delete(input('ids'), 2);
            return BackCode($data['data'], $data['code'], $data['msg']);
        }
    }

    /*单个字段修改*/
    public function update_field()
    {
        if (Request::isAjax() || Request::isPost()) {
            $RuinuoLogic = new RuinuoLogic();
            $data = $RuinuoLogic->UpdateField(input());
            return BackCode($data['data'], $data['code'], $data['msg']);
        }
    }

    /*导入Excel数据*/
    public function import_excel()
    {
        if (Request::isAjax() || Request::isPost()) {
            $file = Request::file("file") ?: Request::file("edit");
            $RuinuoLists = new RuinuoLists();
            $data = $RuinuoLists->InExcel($file);
            return BackCode($data['data'], $data['code'], $data['msg']);
        }
    }

    /*导出Excel数据*/
    public function export_excel()
    {
        $RuinuoLists = new RuinuoLists();
        $RuinuoLists->OutExcel(input());
    }

    /*下载Excel模板*/
    public function template_excel()
    {
        $RuinuoLists = new RuinuoLists();
        $RuinuoLists->TemplateExcel();
    }

    public function Sync()
    {
        if (Request::isAjax() || Request::isPost()) {
            $DataBaseService = new DataBaseService();
            $DataBaseService->Sync(input("table_name"));
            return BackCode([], 200, '同步成功');
        }
    }

    public function Sensitive()
    {
        if (Request::isAjax() || Request::isPost()) {
            $sensitive_field = !empty(input('sensitive_field')) ? json_decode(input('sensitive_field'), true) : [];
            $upData['sensitive_field'] = json_encode($sensitive_field, JSON_UNESCAPED_UNICODE);
            $upData['is_sensitive'] = !empty($sensitive_field) ? 1 : 2;
            $sensitive_num = !empty(input('sensitive_num')) ? json_decode(input('sensitive_num'), true) : [];
            $upData['sensitive_num'] = json_encode($sensitive_num, JSON_UNESCAPED_UNICODE);
            Db::name('ruinuo_database')->where("id='" . input('id') . "'")->update($upData);
            return BackCode([], 200, '设置成功');
        }
        $field = 'id,name,comment,field,sensitive_field,sensitive_num';
        $find = Db::name('ruinuo_database')->where("name='" . input('table_name') . "'")->field($field)->find();
        $listField = !empty($find['field']) ? json_decode($find['field'], true) : [];
        $sensitiveField = !empty($find['sensitive_field']) ? json_decode($find['sensitive_field'], true) : [];
        $sensitiveNum = !empty($find['sensitive_num']) ? json_decode($find['sensitive_num'], true) : [];
        foreach ($listField as $k => $v) {
            $listField[$k]['sensitive_field'] = 2;
            $listField[$k]['sensitive_num'] = 2;
            if (!empty($sensitiveField[$v['field']])) {
                $listField[$k]['sensitive_field'] = 1;
            }
            if (!empty($sensitiveNum[$v['field']])) {
                $listField[$k]['sensitive_num'] = 1;
            }
        }
        View::assign('list_field', $listField);
        View::assign('id', $find['id']);
        View::assign('name', $find['name']);
        View::assign('comment', $find['comment']);
        return ViewTpl('ruinuo/database/sensitive');
    }


}